package TreeNode;


  public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }
 
class TreeNode {
   
public TreeNode lowestCommonAncestor(TreeNode root,TreeNode q,TreeNode p) {
	if(root==null)return null;
    if(root==p||root==q)
		return root;
	
	TreeNode leftRet =lowestCommonAncestor(root.left,q,p) ;
	TreeNode rightRet = lowestCommonAncestor(root.right,q,p);
	if(leftRet!=null&&rightRet!=null) {
		return root;
		}
	else if(leftRet!=null) {
			return leftRet;
	    }
     else if(rightRet!=null) {
	return rightRet;
}
return null;
}
}
